package cn.xhh.dailyreview.server.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;

public class PropertiesDef {

	private static final Logger logger = Logger.getLogger(PropertiesDef.class.getName());
	private static final String PROPERTIES_FILE_NAME = "definition.properties";
	private static final String ENCODING = "UTF-8";

	private static Properties props;
	
	static {// load all the properties values from the .properties file
		loadProperties();
	}

	private static boolean loadProperties() {
		props = new Properties();
		try {
			InputStream is = PropertiesDef.class.getResourceAsStream(PROPERTIES_FILE_NAME);
			if (is == null) {
				throw new IOException("File not found: " + PROPERTIES_FILE_NAME);
			}
			props.load(is);
		} catch (IOException e) {
			logger.severe("Loaing properties failed due to IOException! Exception message: " + 
					e.getMessage());
			return false;
		}
		return true;
	}
	
	public static String[] getDefaultReviewsZh() {
		String[] reviews = get("reviews_zh").split(",");
		for (int i=0; i<reviews.length; i++)
			reviews[i] = reviews[i].trim();
		return reviews;
	}
	
	private static String get(String key) {
		String value = props.getProperty(key);
		if (value == null) return null;
		try {
			return new String(value.getBytes("ISO-8859-1"), ENCODING);
		} catch (UnsupportedEncodingException e) {
			logger.severe("Getting property failed! Unsupported encoding: " + ENCODING);
			return value;
		}
	}
	
}
